From 7b321cdb1673029f42b4d10dcc6c8980091cf13b Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Mon, 2 Jan 2023 12:56:02 -0700 Subject: [PATCH] enforce exclusivity among sort options (#970) * enforce exclusive sort options. * use default brace inits for sort members. --- sort.cc | 130 +++++++++++++++++++++++++++++--------------------------- sort.h | 49 ++++++++++++++------- 2 files changed, 101 insertions(+), 78 deletions(-) diff --git a/sort.cc b/sort.cc index 6c4afecd9..058df4f44 100644 --- a/sort.cc +++ b/sort.cc @@ -70,93 +70,99 @@ bool SortFilter::sort_comp_rh_by_number(const route_head* a, const route_head* b void SortFilter::process() { - if (wpt_sort_mode != SortModeWpt::none) { - switch (wpt_sort_mode) { - case SortModeWpt::description: - waypt_sort(sort_comp_wpt_by_description); - break; - case SortModeWpt::gcid: - waypt_sort(sort_comp_wpt_by_gcid); - break; - case SortModeWpt::shortname: - waypt_sort(sort_comp_wpt_by_shortname); - break; - case SortModeWpt::time: - waypt_sort(sort_comp_wpt_by_time); - break; - default: - fatal(MYNAME ": unknown waypoint sort mode."); - } + switch (wpt_sort_mode) { + case SortModeWpt::none: + break; + case SortModeWpt::description: + waypt_sort(sort_comp_wpt_by_description); + break; + case SortModeWpt::gcid: + waypt_sort(sort_comp_wpt_by_gcid); + break; + case SortModeWpt::shortname: + waypt_sort(sort_comp_wpt_by_shortname); + break; + case SortModeWpt::time: + waypt_sort(sort_comp_wpt_by_time); + break; + default: + fatal(MYNAME ": unknown waypoint sort mode."); } - if (rte_sort_mode != SortModeRteHd::none) { - switch (rte_sort_mode) { - case SortModeRteHd::description: - route_sort(sort_comp_rh_by_description); - break; - case SortModeRteHd::name: - route_sort(sort_comp_rh_by_name); - break; - case SortModeRteHd::number: - route_sort(sort_comp_rh_by_number); - break; - default: - fatal(MYNAME ": unknown route sort mode."); - } + switch (rte_sort_mode) { + case SortModeRteHd::none: + break; + case SortModeRteHd::description: + route_sort(sort_comp_rh_by_description); + break; + case SortModeRteHd::name: + route_sort(sort_comp_rh_by_name); + break; + case SortModeRteHd::number: + route_sort(sort_comp_rh_by_number); + break; + default: + fatal(MYNAME ": unknown route sort mode."); } - if (trk_sort_mode != SortModeRteHd::none) { - switch (trk_sort_mode) { - case SortModeRteHd::description: - track_sort(sort_comp_rh_by_description); - break; - case SortModeRteHd::name: - track_sort(sort_comp_rh_by_name); - break; - case SortModeRteHd::number: - track_sort(sort_comp_rh_by_number); - break; - default: - fatal(MYNAME ": unknown track sort mode."); - } + + switch (trk_sort_mode) { + case SortModeRteHd::none: + break; + case SortModeRteHd::description: + track_sort(sort_comp_rh_by_description); + break; + case SortModeRteHd::name: + track_sort(sort_comp_rh_by_name); + break; + case SortModeRteHd::number: + track_sort(sort_comp_rh_by_number); + break; + default: + fatal(MYNAME ": unknown track sort mode."); } } void SortFilter::init() { // sort waypts by - if (opt_sm_description) { + if (!opt_sm_description && !opt_sm_gcid && !opt_sm_shortname && !opt_sm_time) { + wpt_sort_mode = SortModeWpt::none; + } else if (opt_sm_description && !opt_sm_gcid && !opt_sm_shortname && !opt_sm_time) { wpt_sort_mode = SortModeWpt::description; - } - if (opt_sm_gcid) { + } else if (!opt_sm_description && opt_sm_gcid && !opt_sm_shortname && !opt_sm_time) { wpt_sort_mode = SortModeWpt::gcid; - } - if (opt_sm_shortname) { + } else if (!opt_sm_description && !opt_sm_gcid && opt_sm_shortname && !opt_sm_time) { wpt_sort_mode = SortModeWpt::shortname; - } - if (opt_sm_time) { + } else if (!opt_sm_description && !opt_sm_gcid && !opt_sm_shortname && opt_sm_time) { wpt_sort_mode = SortModeWpt::time; + } else { + fatal(MYNAME ": At most one of the options description, gcid, shortname and time may be selected."); } // sort routes by - if (opt_sm_rtedesc) { + if (!opt_sm_rtedesc && !opt_sm_rtename && !opt_sm_rtenum) { + rte_sort_mode = SortModeRteHd::none; + } else if (opt_sm_rtedesc && !opt_sm_rtename && !opt_sm_rtenum) { rte_sort_mode = SortModeRteHd::description; - } - if (opt_sm_rtename) { + } else if (!opt_sm_rtedesc && opt_sm_rtename && !opt_sm_rtenum) { rte_sort_mode = SortModeRteHd::name; - } - if (opt_sm_rtenum) { + } else if (!opt_sm_rtedesc && !opt_sm_rtename && opt_sm_rtenum) { rte_sort_mode = SortModeRteHd::number; + } else { + fatal(MYNAME ": At most one of the options rtedesc, rtename and rtenum may be selected."); } // sort tracks by - if (opt_sm_trkdesc) { + if (!opt_sm_trkdesc && !opt_sm_trkname && !opt_sm_trknum) { + trk_sort_mode = SortModeRteHd::none; + } else if (opt_sm_trkdesc && !opt_sm_trkname && !opt_sm_trknum) { trk_sort_mode = SortModeRteHd::description; - } - if (opt_sm_trkname) { + } else if (!opt_sm_trkdesc && opt_sm_trkname && !opt_sm_trknum) { trk_sort_mode = SortModeRteHd::name; - } - if (opt_sm_trknum) { + } else if (!opt_sm_trkdesc && !opt_sm_trkname && opt_sm_trknum) { trk_sort_mode = SortModeRteHd::number; + } else { + fatal(MYNAME ": At most one of the options trkdesc, trkname and trknum may be selected."); } } diff --git a/sort.h b/sort.h index 07de160c7..81d64a660 100644 --- a/sort.h +++ b/sort.h @@ -25,14 +25,18 @@ #include // for QString #include // for QVector -#include "defs.h" // for ARGTYPE_BOOL, ARG_NOMINMAX, arglist_t, ARG_TERMI... +#include "defs.h" // for arglist_t, ARGTYPE_BOOL, ARG_NOMINMAX, Waypoint #include "filter.h" // for Filter + #if FILTERS_ENABLED class SortFilter:public Filter { public: + + /* Member Functions */ + QVector* get_args() override { return &args; @@ -41,6 +45,9 @@ public: void process() override; private: + + /* Types */ + enum class SortModeWpt { none, description, @@ -49,8 +56,6 @@ private: time }; - SortModeWpt wpt_sort_mode = SortModeWpt::none; /* How are we sorting these? */ - enum class SortModeRteHd { none, description, @@ -58,12 +63,32 @@ private: number }; - SortModeRteHd rte_sort_mode = SortModeRteHd::none; /* How are we sorting these? */ - SortModeRteHd trk_sort_mode = SortModeRteHd::none; /* How are we sorting these? */ + /* Member Functions */ + + static bool sort_comp_wpt_by_description(const Waypoint* a, const Waypoint* b); + static bool sort_comp_wpt_by_gcid(const Waypoint* a, const Waypoint* b); + static bool sort_comp_wpt_by_shortname(const Waypoint* a, const Waypoint* b); + static bool sort_comp_wpt_by_time(const Waypoint* a, const Waypoint* b); + static bool sort_comp_rh_by_description(const route_head* a, const route_head* b); + static bool sort_comp_rh_by_name(const route_head* a, const route_head* b); + static bool sort_comp_rh_by_number(const route_head* a, const route_head* b); + + /* Data Members */ - char* opt_sm_gcid{}, *opt_sm_shortname{}, *opt_sm_description{}, *opt_sm_time{}; - char* opt_sm_rtenum{}, *opt_sm_rtename{}, *opt_sm_rtedesc{}; - char* opt_sm_trknum{}, *opt_sm_trkname{}, *opt_sm_trkdesc{}; + SortModeWpt wpt_sort_mode{SortModeWpt::none}; /* How are we sorting these? */ + SortModeRteHd rte_sort_mode{SortModeRteHd::none}; /* How are we sorting these? */ + SortModeRteHd trk_sort_mode{SortModeRteHd::none}; /* How are we sorting these? */ + + char* opt_sm_gcid{}; + char* opt_sm_shortname{}; + char* opt_sm_description{}; + char* opt_sm_time{}; + char* opt_sm_rtenum{}; + char* opt_sm_rtename{}; + char* opt_sm_rtedesc{}; + char* opt_sm_trknum{}; + char* opt_sm_trkname{}; + char* opt_sm_trkdesc{}; QVector args = { { @@ -108,14 +133,6 @@ private: }, }; - static bool sort_comp_wpt_by_description(const Waypoint* a, const Waypoint* b); - static bool sort_comp_wpt_by_gcid(const Waypoint* a, const Waypoint* b); - static bool sort_comp_wpt_by_shortname(const Waypoint* a, const Waypoint* b); - static bool sort_comp_wpt_by_time(const Waypoint* a, const Waypoint* b); - static bool sort_comp_rh_by_description(const route_head* a, const route_head* b); - static bool sort_comp_rh_by_name(const route_head* a, const route_head* b); - static bool sort_comp_rh_by_number(const route_head* a, const route_head* b); - }; #endif // FILTERS_ENABLED #endif // SORT_H_INCLUDED_ -- 2.30.2